require.config({
  baseUrl: '../../',
  paths: {
    jquery: 'plugins/jQuery/jquery-2.2.3.min',
    flavr: 'plugins/flavr/flavr.min',
    select2: 'plugins/select2/select2.min',
    echarts: 'plugins/echarts/echarts.min',
    public: 'public/js/public.min'
  },
  shim: {
    flavr: {
      deps: ['jquery']
    },
    select2: {
      deps: ['jquery']
    }
  }
});
require(['jquery', 'echarts', 'flavr', 'select2', 'public'], function ($, echarts) {
  $(function(){
    var jxcb = {
      init: function () {
        this.load();
      },
      load: function () {
        var _this = this;
        $('#header').load('../head.html', function () {
          $('.header-image').hide();
          $('#navigator').load('../left-nav.html', function () {
            _this.year = $('.header-select-year').on('change', function () {
              _this.setTitle();
              _this.queryData();
            });

            _this.initChart();
            _this.setTitle();
            _this.queryData();
          });
        });
      },
      setTitle: function () {
        var year = this.year.find('option:selected').text();
        $('.title').html(year + '年' + GetQueryString('name') + '人员绩效和成本');
      },
      queryData: function () {
        var _this = this;
        publicAjax(basePath + 'personnel-performance', 'post', {
          hospitalId: GetQueryString('id'),
          year: this.year.val()
        }, function (data) {
          console.log(data);
          var xAxisData = [];
          var seriesData = [[], [], [], [], [], []];
          if (data.ret === 1) {
            var list = data.data || [],
              str = '';
            if (list.length) {
              for (var i = 0; i < list.length; i++) {
                xAxisData.push(fixStr(list[i].monthName));
                seriesData[0].push(fixLine2(list[i].annualExpectedHumanCost));
                seriesData[1].push(fixLine2(list[i].humanCost));
                seriesData[2].push(fixLine2(list[i].staffAnnualExpectedWage));
                seriesData[3].push(fixLine2(list[i].staffPerCapitaWage));
                seriesData[4].push(fixLine2(list[i].staffWageImplementationRate));
                seriesData[5].push(fixLine2(list[i].humanCostImplementationRate));

                str += (
                  '<tr>' +
                  '<td>' + fixStr(list[i].monthName) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].staffAnnualExpectedWage) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].staffPerCapitaWage) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].staffWageImplementationRate) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].annualExpectedHumanCost) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].humanCost) + '</td>' +
                  '<td class="text-right">' + fixLine2(list[i].humanCostImplementationRate) + '</td>' +
                  '</tr>'
                );
              }
              _this.chartOption.xAxis.data = xAxisData;
              _this.chartOption.series[0].data = seriesData[0];
              _this.chartOption.series[1].data = seriesData[1];
              _this.chartOption.series[2].data = seriesData[2];
              _this.chartOption.series[3].data = seriesData[3];
              _this.chartOption.series[4].data = seriesData[4];
              _this.chartOption.series[5].data = seriesData[5];
              _this.chart.setOption(_this.chartOption);
              _this.resizeChart();
            } else {
              flavrShowByTime('暂无数据！', null, 'info', false);
              str = '<tr><td colspan="10" class="text-center">暂无数据!</td></tr>';
            }
            $('#table-jxcb').find('tbody').html(str);
          } else {
            flavrShowByTime(data.msg, null, 'error', false);
          }
        });
      },
      chartEl: $('#chart'),
      initChart: function () {
        var _this = this;
        this.chart = echarts.init(this.chartEl[0]);
        this.chart.on('legendselectchanged', function (params) {
          var selectedList = params.selected,
            yAxis = [{show: true},{show: true},{show: true}];
          if (!(selectedList['在编人均绩效工资年度期望值'] || selectedList['在编人均绩效工资'])) {
            yAxis[0].show = false;
          } else {
            yAxis[0].show = true;
          }
          if (!(selectedList['人力成本总额年度期望值'] || selectedList['人力成本总额'])) {
            yAxis[1].show = false;
          } else {
            yAxis[1].show = true;
          }
          if (!(selectedList['在编人均绩效工资执行率'] || selectedList['人力成本预算执行率'])) {
            yAxis[2].show = false;
          } else {
            yAxis[2].show = true;
          }

          if (yAxis[0].show && yAxis[1].show && yAxis[2].show) { // 三更轴全部显示
            yAxis[0].position = 'left';
            yAxis[0].offset = rem;
            yAxis[1].position = 'left';
            yAxis[1].offset = 0;
            yAxis[2].position = 'right';
            yAxis[2].offset = 0;
          } else if (!yAxis[0].show && yAxis[1].show && yAxis[2].show) {
            yAxis[1].position = 'left';
            yAxis[1].offset = 0;
            yAxis[2].position = 'right';
            yAxis[2].offset = 0;
          } else if (yAxis[0].show && !yAxis[1].show && yAxis[2].show) {
            yAxis[0].position = 'left';
            yAxis[0].offset = 0;
            yAxis[2].position = 'right';
            yAxis[2].offset = 0;
          } else if (yAxis[0].show && yAxis[1].show && !yAxis[2].show) {
            yAxis[0].position = 'left';
            yAxis[0].offset = 0;
            yAxis[1].position = 'right';
            yAxis[1].offset = 0;
          } else {
            yAxis[0].position = 'left';
            yAxis[0].offset = 0;
            yAxis[1].position = 'left';
            yAxis[1].offset = 0;
            yAxis[2].position = 'left';
            yAxis[2].offset = 0;
          }
          _this.chart.setOption({yAxis: yAxis});
        });
      },
      resizeChart: function () {
        var chartParent = this.chartEl.parent();
        this.chartEl.width(chartParent[0].clientWidth);
        this.chartEl.height(chartParent[0].clientHeight);
        this.chart.resize();
      },
      chartOption: {
        color: ['#2482b5', '#01b2f5', '#71a76f', '#ace7a4', '#ba5bc1', '#f675a8'],
        tooltip : {
          trigger: 'axis'
        },
        legend: {
          data:['在编人均绩效工资年度期望值','在编人均绩效工资','人力成本总额年度期望值','人力成本总额','在编人均绩效工资执行率','人力成本预算执行率'],
          textStyle: {
            color: '#333'
          }
        },
        xAxis: {
          name: '月份',
          type: 'category',
          data: [],
          nameTextStyle: {
            color: '#333'
          },
          axisLine: {
            lineStyle: {
              color: '#333'
            }
          }
        },
        yAxis: [{
          name: '万元',
          type: 'value',
          offset: rem,
          nameTextStyle: {
            color: '#333'
          },
          axisLine: {
            lineStyle: {
              color: '#333'
            }
          },
          splitLine: {
            show: false
          }
        }, {
          name: '元',
          type: 'value',
          position: 'left',
          nameTextStyle: {
            color: '#333'
          },
          axisLine: {
            lineStyle: {
              color: '#333'
            }
          },
          splitLine: {
            show: false
          }
        }, {
          name: '%',
          type: 'value',
          nameTextStyle: {
            color: '#333'
          },
          axisLine: {
            lineStyle: {
              color: '#333'
            }
          },
          splitLine: {
            show: false
          }
        }],
        series: [{
          name: '在编人均绩效工资年度期望值',
          data: [],
          type: 'bar',
          yAxisIndex: 1,
          barGap: 0
        }, {
          name: '在编人均绩效工资',
          data: [],
          type: 'bar',
          yAxisIndex: 1
        }, {
          name: '人力成本总额年度期望值',
          data: [],
          type: 'bar'
        }, {
          name: '人力成本总额',
          data: [],
          type: 'bar',
          showSymbol: false
        }, {
          name: '在编人均绩效工资执行率',
          data: [],
          type: 'line',
          yAxisIndex: 2
        }, {
          name: '人力成本预算执行率',
          data: [],
          type: 'line',
          showSymbol: false,
          yAxisIndex: 2
        }]
      }
    };

    jxcb.init();
  });
});